﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Layans.Data;
using Layans.Models;

namespace LForum.Web.Controllers
{
    public class PluginController : Controller
    {
        private LForumDb db = new LForumDb();


        public ActionResult Create(Forumpost forums)
        {
            var post = new Forumpost
            {
                Title = Server.HtmlDecode(forums.Title),
                Content = Server.HtmlDecode(forums.Content),
                CreationTime = DateTime.Now,
                
            };

            db.Forumposts.Add(post);
            db.SaveChanges();

            return Redirect("/");
        }

        public ActionResult Post(int id)
        {
            if(!db.Forumposts.Any(x => x.Id == id))
            {
                return Redirect("/");
            }

            var post = db.Forumposts.Where(x => x.Id == id)
                .Select(x => new FullFoRumPostViewModel
                {
                    id = x.Id,
                    Title = x.Title,
                    Content = x.Content,
                    CreationTime = x.CreationTime,
                }).SingleOrDefault();

            return View(post);
        }

    }
}
